﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using DataAccess;
using System.Data.SqlClient;
using System.Configuration;
using System.Text.RegularExpressions;

public partial class Manager_AddQuestion : System.Web.UI.Page
{
    DataTable table;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadCategory();
        }
    }

    private void LoadCategory()
    {
        DbCommand cmd = GenericData.CreateCommand();
        cmd.CommandText = "SELECT * FROM Category";
        table = GenericData.ExecuteReader(cmd);
        ddlCategory.DataSource = table;
        ddlCategory.DataValueField = "ID";
        ddlCategory.DataTextField = "CategoryName";
        ddlCategory.DataBind();
    }


    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string QuestionContent = System.Web.HttpUtility.HtmlEncode(txtQuestion.Text);
        
        string CategoryID = ddlCategory.SelectedValue;
        string Mark = txtMark.Text;
        int typeQuestion = 0;
        if (rdoMultiple.Checked)
            typeQuestion = 1;

        Question question = new Question();
        question.Content = QuestionContent;
        question.CategoryID = int.Parse(CategoryID);
        question.Mark = float.Parse(Mark);
        question.Type = typeQuestion;

        SqlTransaction transaction;
        SqlConnection scon = new SqlConnection("SERVER=.;DATABASE=AptitudeTest;UID=sa;PWD=1234$");
        scon.Open();
        transaction = scon.BeginTransaction();

        try
        {
            int QuestionID = (int)new SqlCommand("INSERT INTO Question(Content, CategoryID, Type, Mark) VALUES('" + question.Content + "', '" + question.CategoryID + "', '" + question.Type + "', '" + question.Mark + "')", scon, transaction).ExecuteNonQuery();
            SqlCommand cmd = new SqlCommand("Select MAX(ID) Question from Question", scon, transaction);
            int ID =(int) cmd.ExecuteScalar();
            string AnswerContent1 = System.Web.HttpUtility.HtmlEncode(txtAnswer1.Text);
            string AnswerContent2 = System.Web.HttpUtility.HtmlEncode(txtAnswer2.Text);
            string AnswerContent3 = System.Web.HttpUtility.HtmlEncode(txtAnswer3.Text);
            string AnswerContent4 = System.Web.HttpUtility.HtmlEncode(txtAnswer4.Text);

            if (rdo1.Checked)
                new SqlCommand("INSERT INTO Answer(QuestionID, Content, IsTrue) VALUES(" + ID + ", '" + AnswerContent1 + "', 1)", scon, transaction).ExecuteNonQuery();
            else
                new SqlCommand("INSERT INTO Answer(QuestionID, Content, IsTrue) VALUES(" + ID + ", '" + AnswerContent1 + "', 0)", scon, transaction).ExecuteNonQuery();

            if (rdo2.Checked)
                new SqlCommand("INSERT INTO Answer(QuestionID, Content, IsTrue) VALUES(" + ID + ", '" + AnswerContent2 + "', 1)", scon, transaction).ExecuteNonQuery();
            else
                new SqlCommand("INSERT INTO Answer(QuestionID, Content, IsTrue) VALUES(" + ID + ", '" + AnswerContent2 + "', 0)", scon, transaction).ExecuteNonQuery();

            if (rdo3.Checked)
                new SqlCommand("INSERT INTO Answer(QuestionID, Content, IsTrue) VALUES(" + ID + ", '" + AnswerContent3 + "', 1)", scon, transaction).ExecuteNonQuery();
            else
                new SqlCommand("INSERT INTO Answer(QuestionID, Content, IsTrue) VALUES(" + ID + ", '" + AnswerContent3 + "', 0)", scon, transaction).ExecuteNonQuery();
            
            if (rdo4.Checked)
                new SqlCommand("INSERT INTO Answer(QuestionID, Content, IsTrue) VALUES(" + ID + ", '" + AnswerContent4 + "', 1)", scon, transaction).ExecuteNonQuery();
            else
                new SqlCommand("INSERT INTO Answer(QuestionID, Content, IsTrue) VALUES(" + ID + ", '" + AnswerContent4 + "', 0)", scon, transaction).ExecuteNonQuery();

            transaction.Commit();
            Session.Add("Message", "Add question successfully");
        }
        catch (SqlException sqlError)
        {
            transaction.Rollback();
            Session.Add("Message", "<font color='red'>Error : " +sqlError.Message + ".Add question unsuccess</font>");
        }
        scon.Close();
        Response.Redirect("ManagerQuestion.aspx");

    }
}